// pages/项目内容/项目内容.js
Page({
  data: {
    project_id: '',
    user_id: '',
    project: {},
    login: {},
    basic_info: {},
    whether_already_attend: false
  },

  onLoad(options) {
    const app = getApp()
    this.setData({
      user_id: app.globalData.user_id,
      project_id: options ? options.project_id : this.data.project_id,
      login: app.globalData.login,
      basic_info: app.globalData.basic_info
    })
    const that = this
    wx.request({
      url: app.globalData.root_url + 'get_project_details/?project_id=' + this.data.project_id,
      method: 'GET',
      success(res) {
        that.setData({
          project: res.data,
          whether_already_attend: res.data.project_participants.includes(that.data.user_id)
        })
      }
    })
  },

  attend_project(event) {
    const that = this
    wx.showModal({
      title: '参加项目',
      content: '确认参加该项目吗？',
      complete: (res) => {
        if (res.confirm) {
          that.setData({
            'project.current_participant_number': that.data.project.current_participant_number,
            'project.project_participants': [...that.data.project.project_participants, that.data.user_id]
          })
          const app = getApp()
          wx.request({
            url: app.globalData.root_url + 'attend_project/?user_id=' + app.globalData.user_id + '&project_id=' + that.data.project_id,
            method: 'POST',
            success: (res) => {
              if (res.data == 1) {
                wx.showModal({
                  title: '参加项目',
                  content: '参加成功',
                  showCancel: false,
                  complete: (res) => {
                    if (res.confirm) {
                      that.onLoad()
                    }
                  }
                })
              }
            }
          })
        }
      }
    })
  },

  quit_project(event) {
    const that = this
    wx.showModal({
      title: '退出项目',
      content: '确认退出该项目吗？',
      complete: (res) => {
        if (res.confirm) {
          that.setData({
            'project.current_participant_number': that.data.project.current_participant_number,
            'project.project_participants': [...that.data.project.project_participants, that.data.user_id]
          })
          const app = getApp()
          wx.request({
            url: app.globalData.root_url + 'quit_project/?user_id=' + app.globalData.user_id + '&project_id=' + that.data.project_id,
            method: 'POST',
            success: (res) => {
              if (res.data == 1) {
                wx.showModal({
                  title: '退出项目',
                  content: '退出成功',
                  showCancel: false,
                  complete: (res) => {
                    if (res.confirm) {
                      that.onLoad()
                    }
                  }
                })
              }
            }
          })
        }
      }
    })
  },

  participants_detail(event) {
    wx.navigateTo({
      url: '/pages/参加人员/参加人员?participants=' + JSON.stringify(this.data.project.project_participants) + '&project_id=' + this.data.project_id,
    })
  },

  drop_project(event) {
    wx.showModal({
      title: '删除项目',
      content: '确认删除此项目吗？',
      complete: (res) => {
        if (res.confirm) {
          const app = getApp()
          wx.request({
            url: app.globalData.root_url + 'drop_project/?user_id=' + app.globalData.user_id + '&project_id=' + this.data.project.id,
            method: 'POST',
            success: (res) => {
              if (res.data == 1) {
                wx.showModal({
                  title: '删除项目',
                  content: '删除成功',
                  showCancel: false,
                  complete: (res) => {
                    if (res.confirm) {
                      wx.navigateBack()
                    }
                  }
                })
              }
            }
          })
        }
      }
    })
  }
})